package com.glsc.ngateway.common.api.flowable.dto.demand;


import com.glsc.ngateway.common.api.common.utils.JsonUtil;
import com.glsc.ngateway.common.api.flowable.dto.param.DoTaskCommentDto;
import com.glsc.ngateway.common.api.flowable.dto.param.ParamDoTaskDto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ParamDoTaskDtoBuilder {


    private String taskId;

    private DoTaskCommentDto comment;

    private ParamCreateSubDtoContent content;
    private String contentstr;

    private String sysid;

    private Map<String, String> taskAssignee;

    private Map<String, String> conditionalVariable;


    public ParamDoTaskDtoBuilder(String taskId, String title, String sysId) {
        this.taskId = taskId;
        this.content = new ParamCreateSubDtoContent();
        this.content.setTitle(title);
        this.sysid = sysId;
        this.taskAssignee = new HashMap<String, String>();
        this.conditionalVariable = new HashMap<String, String>();
    }

    public ParamDoTaskDtoBuilder setSubmit(DemandManagementDto req) {
        this.contentstr = new ParamCreateDtoBuilder().setReqStartElemets(req).build().getContent();
        return this;
    }

    /**
     * 需求确认请求构造
     *
     * @param req
     * @return
     */
    public ParamDoTaskDtoBuilder setConfirmTask(ConfirmTaskReq req) {

        if (req.getPassFlag() == null || req.getPassFlag().equals(PassFlag.reject)) {
            //驳回
            conditionalVariable.put("reject", PassFlag.reject.getNwCode());
        } else {
            //通过审批
            conditionalVariable.put("reject", PassFlag.pass.getNwCode());

            if (req.getRqLevel() != null) {
                this.content.getDemandSubmit().setDemandLevel(req.getRqLevel());
            }
            //技术方案及文档
            content.getDemandSubmit().setSolutionGeneral(req.getSolution());
            if (req.getSolutionFiles() != null) {
                this.content.getDemandSubmit().setSolutionDocument(JsonUtil.Obj2Str(req.getSolutionFiles()));
            }

            //是否需要高级审批,默认不需要，直接到排期
            if (req.getAdvanceFlag() == null) {
                conditionalVariable.put("isAdvance", AdvanceFlag.NOAdvance.getNwCode());
            } else {
                conditionalVariable.put("isAdvance", req.getAdvanceFlag().getNwCode());
            }
        }

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }


    /**
     * 高级审批请求构造
     *
     * @param req
     * @return
     */
    public ParamDoTaskDtoBuilder setAdvanceTask(AdvanceTaskReq req) {


        if (req.getPassFlag() == null || req.getPassFlag().equals(PassFlag.reject)) {
            //驳回，退到需求提交
            conditionalVariable.put("reject", PassFlag.reject.getNwCode());
        } else {
            //通过审批
            conditionalVariable.put("reject", PassFlag.pass.getNwCode());

            if (req.getRqLevel() != null) {
                this.content.getDemandSubmit().setDemandLevel(req.getRqLevel());
            }

            //高级审批下一个节点，默认到排期
            if (req.getAdvanceNext() == null) {
                conditionalVariable.put("advanceNext", AdvanceNext.Arrange.getNwCode());
            } else {
                conditionalVariable.put("advanceNext", req.getAdvanceNext().getNwCode());
                /*会签*/
                if (req.getAdvanceNext().equals(AdvanceNext.SumSign)) {
                    this.taskAssignee.put("signatureGroup", org.apache.tomcat.util.buf.StringUtils.join(req.getSignatureGroup()));
                }
                /*发起OA审批流程*/
                else if (req.getAdvanceNext().equals(AdvanceNext.OA)) {
                    this.taskAssignee.put("oaGroup", org.apache.tomcat.util.buf.StringUtils.join(req.getOaStaffs()));
                }

            }
        }

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }


    public ParamDoTaskDtoBuilder setSignatureTask(SignatureTaskReq req) {

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }

    public ParamDoTaskDtoBuilder setCreateoaTask(CreateoaTaskReq req) {

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }


    public ParamDoTaskDtoBuilder setArrangeTaskReq(ArrangeTaskReq req) {

        //排期相关信息
        content.getDemandSubmit().setArrangeinfo(JsonUtil.Obj2Str(req.getArrangeinfo()));
        taskAssignee.put("demandChargeGroup", org.apache.tomcat.util.buf.StringUtils.join(req.gtDevList()));
        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }

    public ParamDoTaskDtoBuilder setOnlineTaskReq(List<OnlineInf> oldinfo, OnlineTaskReq req) {

        //上线相关信息
        oldinfo.add(req.getOnlineinfo());
        content.getDemandSubmit().setOnlineinfo(JsonUtil.Obj2Str(oldinfo));

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }


    public ParamDoTaskDtoBuilder setCheckTaskReq(CheckTaskReq req) {

        //评审意见
        this.comment = new DoTaskCommentDto();
        comment.setFullMessage(req.getCommnet());

        return this;
    }


    public ParamDoTaskDto build() {
        if (StringUtils.isEmpty(contentstr)) {
            this.contentstr = JsonUtil.Obj2Str(content);
        }
        return new ParamDoTaskDto(this);
    }


    public String getTaskId() {
        return taskId;
    }


    public DoTaskCommentDto getComment() {
        return comment;
    }


    public String getContentstr() {
        return contentstr;
    }


    public String getSysid() {
        return sysid;
    }


    public Map<String, String> getTaskAssignee() {
        return taskAssignee;
    }


    public Map<String, String> getConditionalVariable() {
        return conditionalVariable;
    }


}
